package dev.loong.boot3.controller;

import dev.loong.boot3.pojo.User;
import dev.loong.boot3.service.RabbitMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/rabbitmq")
public class RabbitMqController {
    private final RabbitMqService rabbitMqService;
    @Autowired
    public RabbitMqController(RabbitMqService rabbitMqService)
    {
        this.rabbitMqService = rabbitMqService;
    }
    @GetMapping("/msg")
    public Map<String, Object> msg(String message) {
        rabbitMqService.sendMessage(message);
        return resultMap("message", message);
    }

    @GetMapping("/user")
    public Map<String, Object> user(Long id, String username, String note) {
        var user = new User();
        user.setId(id);
        user.setUserName(username);
        user.setNote(note);
        rabbitMqService.sendUser(user);
        return resultMap("user", user);
    }
    private Map<String, Object> resultMap(String key, Object value) {
        var result = new HashMap<String, Object>();
        result.put("success", true);
        result.put(key, value);
        return result;
    }

}
